using BootstrapBlazorApp.Server.LLMs.messages;
using Newtonsoft.Json;

namespace BootstrapBlazorApp.Server.LLMs.model.Kimi;

public class KimiOutput
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("object")]
    public string Object { get; set; }

    [JsonProperty("created")]
    public long Created { get; set; } // 使用long类型以适应可能的大数值

    [JsonProperty("model")]
    public string Model { get; set; }

    //包含一个choice数组
    [JsonProperty("choices")]
    public Choice[] Choices { get; set; } = Array.Empty<Choice>();

    [JsonProperty("usage")]
    public TokenUsage Usage { get; set; }

    //返回使用的token数量
    public class TokenUsage
    {
        [JsonProperty("prompt_tokens")]
        public int PromptTokens { get; set; }

        [JsonProperty("completion_tokens")]
        public int CompletionTokens { get; set; }

        [JsonProperty("total_tokens")]
        public int TotalTokens { get; set; }
    }

    public class Choice
    {
        [JsonProperty("index")]
        public int Index { get; set; }

        [JsonProperty("message")]
        public ReturnMessage Message { get; set; }

        [JsonProperty("finish_reason")]
        public string FinishReason { get; set; }
        
        //用于接受流式响应
        [JsonProperty("delta")]
        public Delta Delta { get; set; }
    }
    
    public Choice[] GetChoices()
    {
        return Choices;
    }

    public string GetLastContent()
    {
        if (Choices.Length > 0 && Choices[^1].Delta != null)
        {
            return Choices[^1].Delta.Content;
        }
        return string.Empty;
    }
    
    public class Delta
    {
        [JsonProperty("role")]
        public string Role { get; set; }

        [JsonProperty("content")]
        public string Content { get; set; }
    }
}
